package ch.exploadingelephants.graph;

import java.io.Serializable;
import java.util.List;

import ch.exploadingelephants.model.ConveyorDirection;

/**
 * @author Sandro De Zanet
 * 
 */
// @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class,
// property = "@id")
public abstract class Token implements Serializable {

    private static final int MAX_STEPS = 1000;

    /**
     * @param createGraphObject
     * @param direction
     * @return
     */
    public abstract boolean tryConnect(Token createGraphObject,
	    ConveyorDirection direction);

    /**
     * @param inputPattern
     * @param visited
     * @return
     * @throws InfinityLoopException
     */
    public abstract TokenResult accept(DotString inputPattern,
	    List<VisitedNodeState> visited) throws InfinityLoopException;

    protected void checkForInfiniteLoop(DotString inputPattern,
	    List<VisitedNodeState> visited) throws InfinityLoopException {
	VisitedNodeState newState = new VisitedNodeState(this, inputPattern);
	if (visited.size() > MAX_STEPS || visited.contains(newState)) {
	    throw new InfinityLoopException();
	} else {
	    visited.add(newState);
	}
    }

}
